#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <math.h>

#include "FAT32Pref.h"

using namespace std;

//Constructor:
FAT32Pref::FAT32Pref(unsigned int maximalSize, unsigned int blockSize,
		unsigned int pointerSize) : SingleFilePref(maximalSize,blockSize,pointerSize){
}


//Measuring the performance of a single file with the given file in a FAT 32 file system:
int FAT32Pref::singleFileSystemPerformance(unsigned int fileSize,outputStruct *output){

	//calculating the space needed for storing the file:
	unsigned int blockSizeForData = _blockSize - _pointerSize;
	unsigned int numOfBlocks = ceil((double)fileSize / (double)blockSizeForData);
	output->space = numOfBlocks*_blockSize;

	//calculating average number of block accesses:
	double totalAccessNum = 0;

	//all the blocks except the last one are full:
	for (unsigned int i=1; i<numOfBlocks; i++)
	{
		totalAccessNum += i*blockSizeForData;
	}
	//adding last block bytes (block might be only partially full):
	totalAccessNum += (fileSize - (blockSizeForData*(numOfBlocks-1)))*numOfBlocks;

	output->averageAccessNum = totalAccessNum/fileSize;
	return SUCCESS;
}

//Checks whether the file system can operate under the given constraints.
int FAT32Pref::areConstraintsPossilble(){
	//The system constraints are not possible if the pointer exceeds the block size:
	if(_pointerSize>=_blockSize)
	{
		return FAILURE;
	}
	else{
		return SUCCESS;
	}
}



